package sw6.app4;

import java.util.ArrayList;

import sw6.lib.exceptions.SettingNotFoundException;
import sw6.lib.internal.debug.*;
import sw6.lib.types.Interval;
import sw6.lib.Settings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Tests install of sw6.app4v1.
 * These tests should be seen as a replace of the libtest project, 
 * as every test described in libtest, will be executed here instead.
 * This is done, as the only way of inserting settings into the db is 
 * through the settings.xml file.
 * @author sw6b
 */
public class TestActivity extends Activity {
	
	private static final String APP_NAME 	= "sw6.app4v1";
	private int testNumber 					= 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Define the resulting intent
		Intent resultIntent 		= new Intent();
		ArrayList<Boolean> results 	= new ArrayList<Boolean>();
		
		/**
		 * BOOLEAN GET/SET TEST
		 */
		
		String varName = "boolean1";
		
		// TEST NO. 0
		try {
			boolean value		= Settings.getBoolean(this, varName);
			Settings.setBoolean(this, varName, !value);
			boolean expected	= !value;
			value				= Settings.getBoolean(this, varName);

			if(value == expected) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected + ", was: " + value);
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;
		
		
		/**
		 * DOUBLE GET/SET
		 */
		
		varName = "double1";
		
		// TEST NO. 1
		try {
			double garbage	= 33.589545;
			Settings.setDouble(this, varName, garbage);
			double expected = 18.6512699;
			Settings.setDouble(this, varName, expected);
			double value	= Settings.getDouble(this, varName);

			if(value == expected) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected + ", was: " + value);
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;
		
		/**
		 * INTEGER GET/SET
		 */
		
		varName = "int1";
		
		// TEST NO. 2
		try {
			int garbage		= 132;
			Settings.setInteger(this, varName, garbage);
			int expected 	= 12;
			Settings.setInteger(this, varName, expected);
			double value	= Settings.getInteger(this, varName);

			if(value == expected) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected + ", was: " + value);
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;
		
		/**
		 * STRING GET/SET
		 */
		
		varName = "string1";
		
		// TEST NO. 3
		try {
			String garbage		= "##¤(/¤%&/(!=((#)#(?";
			Settings.setString(this, varName, garbage);
			String expected 	= "followSw6bOnTwitter....";
			Settings.setString(this, varName, expected);
			String value		= Settings.getString(this, varName);

			if(value.equals(expected)) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected + ", was: " + value);
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;
		
		/**
		 * ENUM GET/SET
		 */
		
		varName = "enum1";
		
		// TEST NO. 4
		try {
			int garbage		= 2;
			Settings.setEnum(this, varName, garbage);
			int expected 	= 3;
			Settings.setEnum(this, varName, expected);
			int value		= Settings.getEnum(this, varName);

			if(value == expected) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected + ", was: " + value);
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;

		/**
		 * GETTER EXCEPTION - PRIMITIVES
		 */
		
		varName = "garbage";
	
		// TEST NO. 5
		boolean hasConcludedGetExceptionTest = false;
		try {
			Settings.getInteger(this, varName);
		} catch(SettingNotFoundException e) {
			hasConcludedGetExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedGetExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * SETTER EXCEPTION - PRIMITIVES
		 */
		
		varName = "garbage";
	
		// TEST NO. 6
		boolean hasConcludedSetExceptionTest = false;
		try {
			Settings.setInteger(this, varName, 1337);
		} catch(SettingNotFoundException e) {
			hasConcludedSetExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedSetExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * GETTER EXCEPTION - OBJECTS
		 */
		
		varName = "garbage";
	
		// TEST NO. 7
		boolean hasConcludedGetObjectExceptionTest = false;
		try {
			Settings.getObject(this, varName, Interval.class);
		} catch(SettingNotFoundException e) {
			hasConcludedGetObjectExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedGetObjectExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * SETTER EXCEPTION - OBJECTS
		 */
		
		varName = "garbage";
	
		// TEST NO. 8
		boolean hasConcludedSetObjectExceptionTest = false;
		try {
			Settings.setObject(this, varName, 123);
		} catch(SettingNotFoundException e) {
			hasConcludedSetObjectExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedSetObjectExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		
		/**
		 * GETTER EXCEPTION - STDOBJECTS
		 */
		
		varName = "garbage";
	
		// TEST NO. 9
		boolean hasConcludedGetStdObjectExceptionTest = false;
		try {
			Settings.getStdObject(this, varName, Interval.class);
		} catch(SettingNotFoundException e) {
			hasConcludedGetStdObjectExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedGetStdObjectExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * SETTER EXCEPTION - STDOBJECTS
		 */
		
		varName = "garbage";
	
		// TEST NO. 10
		boolean hasConcludedSetStdObjectExceptionTest = false;
		try {
			Settings.setStdObject(this, varName, 123);
		} catch(SettingNotFoundException e) {
			hasConcludedSetStdObjectExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedSetStdObjectExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * ENUM CONSTRAINT EXCEPTION TEST
		 */
		
		varName = "enum1";
	
		// TEST NO. 11
		boolean hasConcludedSetEnumExceptionTest = false;
		try {
			Settings.setEnum(this, varName, -1);
		} catch(SettingNotFoundException e) {
			hasConcludedSetEnumExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedSetEnumExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * INT MAX CONSTRAINT EXCEPTION TEST
		 */
		
		varName = "int1";
	
		// TEST NO. 12
		boolean hasConcludedViolateIntConstraintsExceptionTest = false;
		try {
			Settings.setInteger(this, varName, 3000);
		} catch(SettingNotFoundException e) {
			hasConcludedViolateIntConstraintsExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedViolateIntConstraintsExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * INT MIN CONSTRAINT EXCEPTION TEST
		 */
		
		varName = "int1";
	
		// TEST NO. 13
		boolean hasConcludedViolateMinIntConstraintsExceptionTest = false;
		try {
			Settings.setInteger(this, varName, -1000);
		} catch(SettingNotFoundException e) {
			hasConcludedViolateMinIntConstraintsExceptionTest = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedViolateMinIntConstraintsExceptionTest) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		/**
		 * OBJECT GET/SET
		 */
		
		varName = "object1";
		
		// TEST NO. 14
		try {
			StringBuilder garbage 	= new StringBuilder();
			garbage.append(1337);
			Settings.setObject(this, varName, garbage);
			StringBuilder expected 	= new StringBuilder();
			expected.append(444333);
			Settings.setObject(this, varName, expected);
			StringBuilder value = Settings.getObject(this, varName, StringBuilder.class);

			if(value.toString().equals(expected.toString())) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected.toString() + ", was: " + value.toString());
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		testNumber++;
		
		/**
		 * STDOBJECT GET/SET
		 */
		
		varName = "stdobject1";
		
		// TEST NO. 15
		try {
			Interval garbage = new Interval(10, 100, 0, 1000);
			Settings.setStdObject(this, varName, garbage);
			Interval expected 	= new Interval(5, 99, 0, 1000);
			Settings.setStdObject(this, varName, expected);
			Interval value = Settings.getStdObject(this, varName, Interval.class);

			if(expected.equals(value)) {
				DebugHelpers.logPass(testNumber, APP_NAME);
				results.add(true);
			} else {
				throw new Exception("Expected: " + expected.toString() + ", was: " + value.toString());
			}
		} catch(Exception e) {
			DebugHelpers.logIssue(testNumber, APP_NAME, e.getMessage());
			results.add(false);
		}
		
		resultIntent.putExtra("appName", this.getString(R.string.app_name));
		resultIntent.putExtra("results", DebugHelpers.getBooleanArray(results));
		setResult(RESULT_OK, resultIntent);
		finish();
	}
}
